from rest_framework import serializers
from inventory.models import (
    Warehouse,
    WarehouseStatus,
    Location,
    LocationStatus,
    Inventory,
    InventoryTransaction,
    TransactionType,
    InventoryCheck,
    InventoryCheckStatus,
    InventoryCheckDetail
)
from utils.serializers import CustomModelSerializer
from product.models import Product


class WarehouseSerializer(CustomModelSerializer):
    """
    仓库信息序列化器，用于仓库信息的序列化与反序列化
    """
    status = serializers.ChoiceField(choices=WarehouseStatus.choices, help_text="仓库状态")
    is_primary = serializers.BooleanField(required=False, help_text="是否主仓库")
    capacity = serializers.IntegerField(required=False, help_text="仓库容量")
    area = serializers.DecimalField(max_digits=10, decimal_places=2, required=False, help_text="仓库面积")
    manager_id = serializers.IntegerField(required=False, help_text="仓库管理员ID")
    contact_person = serializers.CharField(max_length=50, required=False, help_text="联系人")
    contact_phone = serializers.CharField(max_length=20, required=False, help_text="联系电话")
    address = serializers.CharField(max_length=255, required=False, help_text="仓库地址")
    description = serializers.CharField(required=False, help_text="仓库描述")
    
    class Meta:
        model = Warehouse
        fields = '__all__'
        read_only_fields = ['create_time', 'update_time', 'warehouse_id']


class LocationSerializer(CustomModelSerializer):
    """
    库位信息序列化器，用于库位信息的序列化与反序列化
    """
    status = serializers.ChoiceField(choices=LocationStatus.choices, help_text="库位状态")
    warehouse = serializers.PrimaryKeyRelatedField(queryset=Warehouse.objects.filter(is_deleted=False), help_text="所属仓库")
    is_aisle = serializers.BooleanField(help_text="是否为通道")
    capacity = serializers.IntegerField(required=False, help_text="库位容量")
    zone = serializers.CharField(max_length=50, required=False, help_text="库区")
    row = serializers.CharField(max_length=10, required=False, help_text="排")
    column = serializers.CharField(max_length=10, required=False, help_text="列")
    level = serializers.CharField(max_length=10, required=False, help_text="层")
    location_name = serializers.CharField(max_length=100, required=False, help_text="库位名称")
    location_type = serializers.CharField(max_length=50, required=False, help_text="库位类型")
    description = serializers.CharField(required=False, help_text="库位描述")
    
    class Meta:
        model = Location
        fields = '__all__'
        read_only_fields = ['create_time', 'update_time', 'location_id', 'current_load']


class InventorySerializer(CustomModelSerializer):
    """
    库存信息序列化器，用于库存信息的序列化与反序列化
    """
    product = serializers.PrimaryKeyRelatedField(queryset=Product.objects.filter(is_deleted=False), help_text="产品")
    warehouse = serializers.PrimaryKeyRelatedField(queryset=Warehouse.objects.filter(is_deleted=False), help_text="仓库")
    location = serializers.PrimaryKeyRelatedField(queryset=Location.objects.filter(is_deleted=False), help_text="库位")
    quantity = serializers.DecimalField(max_digits=14, decimal_places=4, help_text="库存数量")
    available_quantity = serializers.DecimalField(max_digits=14, decimal_places=4, help_text="可用数量")
    reserved_quantity = serializers.DecimalField(max_digits=14, decimal_places=4, help_text="预留数量")
    unit_cost = serializers.DecimalField(max_digits=14, decimal_places=4, required=False, help_text="单位成本")
    lot_number = serializers.CharField(max_length=50, required=False, help_text="批次号")
    serial_number = serializers.CharField(max_length=100, required=False, help_text="序列号")
    expiry_date = serializers.DateField(required=False, help_text="过期日期")
    manufactured_date = serializers.DateField(required=False, help_text="生产日期")
    
    class Meta:
        model = Inventory
        fields = '__all__'
        read_only_fields = ['create_time', 'update_time', 'inventory_id']


class InventoryTransactionSerializer(CustomModelSerializer):
    """
    库存交易记录序列化器，用于库存交易记录的序列化与反序列化
    """
    transaction_type = serializers.ChoiceField(choices=TransactionType.choices, help_text="交易类型")
    product = serializers.PrimaryKeyRelatedField(queryset=Product.objects.filter(is_deleted=False), help_text="产品")
    from_warehouse = serializers.PrimaryKeyRelatedField(queryset=Warehouse.objects.filter(is_deleted=False), required=False, help_text="源仓库")
    to_warehouse = serializers.PrimaryKeyRelatedField(queryset=Warehouse.objects.filter(is_deleted=False), required=False, help_text="目标仓库")
    from_location = serializers.PrimaryKeyRelatedField(queryset=Location.objects.filter(is_deleted=False), required=False, help_text="源库位")
    to_location = serializers.PrimaryKeyRelatedField(queryset=Location.objects.filter(is_deleted=False), required=False, help_text="目标库位")
    quantity = serializers.DecimalField(max_digits=14, decimal_places=4, help_text="交易数量")
    unit_cost = serializers.DecimalField(max_digits=14, decimal_places=4, required=False, help_text="交易单位成本")
    total_cost = serializers.DecimalField(max_digits=14, decimal_places=4, required=False, help_text="交易总成本")
    reference_document = serializers.CharField(max_length=100, required=False, help_text="参考单据")
    reference_type = serializers.CharField(max_length=50, required=False, help_text="参考类型")
    lot_number = serializers.CharField(max_length=50, required=False, help_text="批次号")
    serial_number = serializers.CharField(max_length=100, required=False, help_text="序列号")
    remark = serializers.CharField(required=False, help_text="交易备注")
    
    class Meta:
        model = InventoryTransaction
        fields = '__all__'
        read_only_fields = ['create_time', 'update_time', 'transaction_id']


class InventoryCheckSerializer(CustomModelSerializer):
    """
    库存盘点序列化器，用于库存盘点信息的序列化与反序列化
    """
    status = serializers.ChoiceField(choices=InventoryCheckStatus.choices, help_text="盘点状态")
    warehouse = serializers.PrimaryKeyRelatedField(queryset=Warehouse.objects.filter(is_deleted=False), help_text="盘点仓库")
    check_date = serializers.DateField(help_text="盘点日期")
    checker_id = serializers.IntegerField(help_text="盘点人ID")
    approver_id = serializers.IntegerField(required=False, help_text="审批人ID")
    approval_date = serializers.DateTimeField(required=False, help_text="审批日期")
    start_time = serializers.DateTimeField(required=False, help_text="开始时间")
    end_time = serializers.DateTimeField(required=False, help_text="结束时间")
    total_variance_quantity = serializers.DecimalField(max_digits=14, decimal_places=4, help_text="总数量差异")
    total_variance_amount = serializers.DecimalField(max_digits=14, decimal_places=4, help_text="总金额差异")
    remark = serializers.CharField(required=False, help_text="盘点备注")
    
    class Meta:
        model = InventoryCheck
        fields = '__all__'
        read_only_fields = ['create_time', 'update_time', 'check_id']


class InventoryCheckDetailSerializer(CustomModelSerializer):
    """
    库存盘点明细序列化器，用于库存盘点明细信息的序列化与反序列化
    """
    inventory_check = serializers.PrimaryKeyRelatedField(queryset=InventoryCheck.objects.filter(is_deleted=False), help_text="盘点单")
    product = serializers.PrimaryKeyRelatedField(queryset=Product.objects.filter(is_deleted=False), help_text="产品")
    location = serializers.PrimaryKeyRelatedField(queryset=Location.objects.filter(is_deleted=False), help_text="库位")
    system_quantity = serializers.DecimalField(max_digits=14, decimal_places=4, help_text="系统数量")
    actual_quantity = serializers.DecimalField(max_digits=14, decimal_places=4, help_text="实际数量")
    # 计算字段 - 确保设置为只读
    variance_quantity = serializers.DecimalField(max_digits=14, decimal_places=4, read_only=True, help_text="数量差异")
    unit_cost = serializers.DecimalField(max_digits=14, decimal_places=4, required=False, help_text="单位成本")
    # 计算字段 - 确保设置为只读
    variance_amount = serializers.DecimalField(max_digits=14, decimal_places=4, read_only=True, help_text="金额差异")
    lot_number = serializers.CharField(max_length=50, required=False, help_text="批次号")
    serial_number = serializers.CharField(max_length=100, required=False, help_text="序列号")
    remark = serializers.CharField(required=False, help_text="明细备注")
    
    class Meta:
        model = InventoryCheckDetail
        fields = '__all__'
        read_only_fields = ['create_time', 'update_time', 'detail_id', 'variance_quantity', 'variance_amount']